﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevEx.Thanh.Class;
using DevExpress.XtraEditors;

namespace DevEx.Thanh.UserControl
{
    public partial class ucSoKho : DevExpress.XtraEditors.XtraUserControl
    {
        private CTonController ctrlTon = new CTonController();
        public ucSoKho()
        {
            InitializeComponent();
        }

        private void ucSoKho_Load(object sender, EventArgs e)
        {
            //lay du lieu kho
            CViewer.BindKhoToLookupEdit(lkeKho);
        }

        private void lkeKho_EditValueChanged(object sender, EventArgs e)
        {
            int soMatHang = 0;
            //hiển thị danh sách vật tư
            try
            {
                int maKho = Convert.ToInt32(lkeKho.EditValue);
                gridVatTu.DataSource = ctrlTon.GetTonKho2( maKho);
            }
            catch (Exception)
            {
                gridVatTu.DataSource = null;
            }
           
            //lấy số lượng
            soMatHang = gridView1.RowCount;
            lblTongMatHang.Text = "Hiện có: " + soMatHang.ToString( ) + " mặt hàng";
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                //xoa trang thong tin
                foreach (Control c in panelThongTinVatTu.Controls)
                {
                    if (c.GetType() == typeof(TextEdit))
                    {
                        TextEdit text = (TextEdit)c;
                        text.EditValue = "";
                        text.Properties.ReadOnly = true;
                    }
                }
                gridTTNhapXuat.DataSource = null;
                try
                {
                    //lấy thông tin mã mặt hàng
                    int idMatHang = (int)gridView1.GetFocusedRowCellValue("ID_MatHang");
                    //lấy thông tin tồn
                    tblTon curMatHang = ctrlTon.SelectRecordById(idMatHang);
                    if (curMatHang != null)
                    {
                        //hiển thị thông tin chi tiết
                        txtTenVT.EditValue = curMatHang.tblVatTu.VatTu;
                        txtKyMaHieu.EditValue = curMatHang.tblVatTu.MaKyHieu;
                        txtSoTheKho_Code.EditValue = curMatHang.SoTheKho + "-" + curMatHang.Code; 
                        if (curMatHang.Gia.HasValue)
                        {
                            txtGiaLe.EditValue = curMatHang.Gia.Value.ToString("#,##0.##");
                        }
                        txtDVT.EditValue = curMatHang.tblVatTu.tblDVT.DVT;
                        txtCCL.EditValue = curMatHang.tblCCL.CCL;
                        txtDauKy.EditValue = curMatHang.SLKK11.GetValueOrDefault( );

                        //hiển thị thông tin nhập xuất của mặt hàng
                        HienThiThongTinNhapXuat(curMatHang.ID_MatHang, curMatHang.SLKK11.GetValueOrDefault());
                        //gridTTNhapXuat.DataSource = ctrlTon.GetTTNhapXuat(curMatHang.ID_MatHang, curMatHang.SLKK11);
                    }
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }

        private void HienThiThongTinNhapXuat(int id_MatHang, double tonDauKy)
        {
            DataTable dt = new DataTable();
            string cauLenh = "select * from fc_T_NhapXuatMatHang(" + id_MatHang.ToString( ) + ")";
            DevEx.Thanh.Utilities.XL_DULIEU.LayDuLieuRaBang(cauLenh, ref dt);
            double ton = tonDauKy;
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                DataRow dr = dt.Rows[i];
                double gia = 0;
                double nhap = 0;
                double xuat = 0;
                try
                {
                    gia = Double.Parse(dr["Gia"].ToString());
                    nhap = Double.Parse(dr["SLNhap"].ToString());
                    xuat = Double.Parse(dr["SLXuat"].ToString());
                }
                catch (Exception){}
                
                ton = ton + nhap - xuat;
                double tienTon = ton * gia;
                dr["SlTon"] = ton;
                dr["TienTon"] = tienTon;}
            gridTTNhapXuat.DataSource  = dt;

        }

        private void bandedGridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "SLNhap" || e.Column.FieldName == "SLXuat" || e.Column.FieldName == "TienNhap" || e.Column.FieldName == "TienXuat" || e.Column.FieldName == "SLTon" || e.Column.FieldName == "TienTon")
            {
                if (e.Value.ToString() !="")
                {
                    if (Convert.ToDouble(e.Value) == 0)
                    {
                        e.DisplayText = "";
                    }
                }
                
            }
        }

        private void btInTheKho_Click(object sender, EventArgs e)
        {
            int idMatHang = (int)gridView1.GetFocusedRowCellValue("ID_MatHang");
            CViewer.InSoKho(idMatHang);
        }
    }}
